<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/11/8
 * Time: 9:46
 */
namespace app\admin\controller;
use think\Db;
class Agent extends Base {
    public function index(){
        if(request()->isAjax()){
            $where = array();
            $params = input('param.');
//            if(isset($params['coupon_status']) && $params['coupon_status'] != '')
//            {
//                $where['coupon_status'] = $params['coupon_status'];
//            }
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['a.dl_phone'] = ['like', "%" . $params['key'] . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('dl')->alias('a')
                ->where($where)
                 ->count();// 获取总条数
            $lists = Db::name('dl')->alias('a')
                ->page($Nowpage,$limits)
                ->where($where)
                ->select();
            foreach ($lists as $key=>&$vo){
                $sheng = Db::table('up_area')->where('district_id',$vo['dl_sheng'])->value('district');
                $shi = Db::table('up_area')->where('district_id',$vo['dl_shi'])->value('district');
                $vo['address'] = $sheng.$shi;
                $vo['num'] = count(array_filter(explode(',',$vo['dl_num'])));
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?activity_id')){
            session('activity_id',null);
        }
        return $this->fetch();
    }
    public function detail(){
        if(request()->isAjax()){
//            $where = array();
//            $params = input('param.');
//            if(isset($params['coupon_status']) && $params['coupon_status'] != '')
//            {
//                $where['coupon_status'] = $params['coupon_status'];
//            }
//            if(isset($params['key']) && $params['key'] != '')
//            {
//                $where['coupon_name'] = ['like', "%" . $params['key'] . "%"];
//            }
            $res = Db::table('up_dl')->where('dl_id',input('param.id'))->find();
            $where['s_id'] = array('IN',$res['dl_num']);
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('shops')->alias('a')
                 ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('shops')->alias('a')
                ->page($Nowpage,$limits)
                ->where($where)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?activity_id')){
            session('activity_id',input('id'));
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 充值
     */
    public function recharge(){
        if(request()->isPost()){
            $id = input('param.id');
            $res = Db::table('up_dl')->where('dl_id', $id)->setInc('dl_balance',input('param.money'));
            if($res){
                $insert = [
                    'dlmx_dl_id' => $id,
                    'dlmx_money' => input('param.money'),
                    'dlmx_ptb' => input('param.money')*10,
                    'dlmx_admin' => session('username'),
                    'dlmx_time' => date('Y-m-d H:i:s')
                ];
                Db::table('up_dlmx')->insert($insert);
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.id');
        $this->assign('id',$id);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $length = mb_strlen(input('param.account'),'UTF8');
            if($length<6){
                return json(['code' => 100, 'msg' => '账号不能低于6位']);
            }
            if($length>16){
                return json(['code' => 100, 'msg' => '账号不能大于16位']);
            }
            $pattern = '/^1[3456789]{1}\d{9}$/';
            if (preg_match($pattern, input('param.phone'), $matches)) {
            } else {
                return json(['code' => 100, 'msg' => '手机号码格式不对']);
            }
            $have = Db::table('up_dl')
                ->where('dl_account',input('param.account'))
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '账号重复']);
            }
            $data['dl_name'] = input('param.name');
            $data['dl_phone'] = input('param.phone');
            $data['dl_account'] = input('param.account');
            $data['dl_password'] = md5(input('param.password'));
            $data['password'] = input('param.password');
            $data['dl_balance'] = input('param.balance');
            $data['dl_num'] = input('param.assn_user_id');
            $data['dl_sheng'] = input('param.province');
            $data['dl_shi'] = input('param.city');
            $data['dl_num'] = input('param.assn_user_id');
            $data['dl_time'] = date('Y-m-d H:i:s',time());
            $data['dl_level'] = input('param.level');
//            $data['dl_shop_id'] = session('shop_id');
//            $data['dl_intro'] = $s_dl_content;
            $data['dl_status'] = 2;
            $res = Db::table('up_dl')->insertGetId($data);
//            $time = date('Y-m-d H:i:s',time());
//            Db::table('up_shops')->where('s_id','IN',$data['dl_num'])->setField('s_dl_time',$time);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $shop = Db::table('up_shops')->where('s_status',2)->select();
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
            'shop'=>$shop
        ]);
        return $this->fetch();
    }

    /***
     * @return mixed
     * 编辑
     */
    public function update(){
        if(request()->isPost()){
            $length = mb_strlen(input('param.account'),'UTF8');
            if($length<6){
                return json(['code' => 100, 'msg' => '账号不能低于6位']);
            }
            if($length>16){
                return json(['code' => 100, 'msg' => '账号不能大于16位']);
            }
            $pattern = '/^1[3456789]{1}\d{9}$/';
            if (preg_match($pattern, input('param.phone'), $matches)) {
            } else {
                return json(['code' => 100, 'msg' => '手机号码格式不对']);
            }
            $have = Db::table('up_dl')
                ->where('dl_account',input('param.account'))
                ->where('dl_id','<>',input('param.id'))
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '账号重复']);
            }
//            $s_dl_content = Db::table('up_shops')->where('s_id',session('shop_id'))->value('s_dl_content');
            $data['dl_name'] = input('param.name');
            $data['dl_phone'] = input('param.phone');
            $data['dl_account'] = input('param.account');
            $data['dl_password'] = md5(input('param.password'));
            $data['password'] = input('param.password');
            $data['dl_balance'] = input('param.balance');
            $data['dl_num'] = input('param.assn_user_id');
            $data['dl_sheng'] = input('param.province');
            $data['dl_shi'] = input('param.city');
            $data['dl_qu'] = input('param.district');
            $data['dl_num'] = input('param.assn_user_id');
            $data['dl_num'] = input('param.assn_user_id');
            $data['dl_level'] = input('param.level');
            $res = Db::table('up_dl')->where('dl_id',input('param.id'))->update($data);
            $time = date('Y-m-d H:i:s',time());
            Db::table('up_shops')->where('s_id','IN',$data['dl_num'])->setField('s_dl_time',$time);
            //更新shop表
            $res1 = Db::table('up_dl')->where('dl_id',input('param.id'))->find();
            if($res1['d_f_id']!=''){
                $data1['s_dl_name'] = $res1['dl_name'];
                $data1['s_dl_phone'] = $res1['dl_phone'];
                $data1['s_dl_sheng'] = $res1['dl_sheng'];
                $data1['s_dl_shi'] = $res1['dl_shi'];
                $data1['s_dl_qu'] = $res1['dl_qu'];
                $data1['s_dl_level'] = $res1['dl_level'];
                Db::table('up_shops')->where('s_id',$res1['d_f_id'])->update($data1);
            }
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $shop = Db::table('up_shops')->where('s_status',2)->select();
        $province = Db::table('up_area')->where('level', 1)->select();

        $id = input('param.id');
        $assn = Db::table('up_dl')->where('dl_id', $id)->find();
        $assn['num'] = explode(',',$assn['dl_num']);
        $city = Db::table('up_area')->where('pid', $assn['dl_sheng'])->select();
        $district = Db::table('up_area')->where('pid', $assn['dl_shi'])->select();
        $this->assign([
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'shop'=>$shop,
            'id'=>$id,
            'assn'=>$assn
        ]);
        return $this->fetch();
    }
}